package dao

import (
	"bms/db"
	"bms/model"
)

func GetBook() []*model.Book {
	sql := "select id,title,author,publish,price,borrow,remain,img_path from books"
	rows, _ := db.Db.Query(sql)
	var books []*model.Book
	for rows.Next() {
		book := &model.Book{}
		rows.Scan(&book.Id, &book.Title, &book.Author, &book.Publish, &book.Price, &book.Borrow, &book.Remain, &book.ImgPath)
		books = append(books, book)
	}
	return books
}

func GetBookByPage(currentPage int64) *model.Page {
	//查询数据库里一共多少条
	sql := "select count(*) from books"
	row := db.Db.QueryRow(sql)
	var totalRows int64
	row.Scan(&totalRows)
	//每页限制几条数据
	var rows int64 = 4
	//一共分为多少页,奇数页或偶数页
	var totalPages int64
	if totalRows%rows == 0 {
		totalPages = totalRows / rows
	} else {
		totalPages = totalRows/rows + 1
	}

	//分页查询图书
	sql1 := "select id,title,author,publish,price,borrow,remain,img_path,is_borrow from books limit ?,?"
	bookRows, err := db.Db.Query(sql1, (currentPage-1)*rows, rows)
	if err != nil {
		return nil
	}
	var books []*model.Book
	for bookRows.Next() {
		book := &model.Book{}
		bookRows.Scan(&book.Id, &book.Title, &book.Author, &book.Publish, &book.Price, &book.Borrow, &book.Remain, &book.ImgPath, &book.IsBorrow)
		books = append(books, book)
	}
	page := &model.Page{
		CurrentPage: currentPage,
		TotalRows:   totalRows,
		Rows:        rows,
		TotalPages:  totalPages,
		Book:        books,
	}
	return page
}

func GetBookByTitleOrAuthor(currentPage int64, title, author string) *model.Page {
	//查询数据库里一共多少条
	sql := "select count(*) from books where title like ? or title like ? or title like ? or author = ?"
	row := db.Db.QueryRow(sql, title+"%", "%"+title+"%", "%"+title, author)
	var totalRows int64
	row.Scan(&totalRows)
	//每页限制几条数据
	var rows int64 = 4
	//一共分为多少页,奇数页或偶数页
	var totalPages int64
	if totalRows%rows == 0 {
		totalPages = totalRows / rows
	} else {
		totalPages = totalRows/rows + 1
	}
	//根据书名或者作者名进行查询
	sql1 := "select id,title,author,publish,price,borrow,remain,img_path,is_borrow from books where title like ? or title like ? or title like ? or author = ? limit ?,?"
	bookRows, _ := db.Db.Query(sql1, title+"%", "%"+title+"%", "%"+title, author, rows*(currentPage-1), rows)
	var books []*model.Book
	for bookRows.Next() {
		book := &model.Book{}
		bookRows.Scan(&book.Id, &book.Title, &book.Author, &book.Publish, &book.Price, &book.Borrow, &book.Remain, &book.ImgPath, &book.IsBorrow)
		books = append(books, book)
	}
	page := &model.Page{
		CurrentPage: currentPage,
		TotalRows:   totalRows,
		Rows:        rows,
		TotalPages:  totalPages,
		Book:        books,
	}
	return page
}

func BorrowBook(bookId int64) (*model.Book, error) {
	sql := "select id,title,author,publish,price,borrow,remain,img_path from books where id=?"
	row := db.Db.QueryRow(sql, bookId)
	book := &model.Book{}
	err := row.Scan(&book.Id, &book.Title, &book.Author, &book.Publish, &book.Price, &book.Borrow, &book.Remain, &book.ImgPath)
	if err != nil {
		return nil, err
	}
	return book, nil
}

func QueryBookById(bookId int64) *model.Book {
	sql := "select id,title,author,publish,price,borrow,remain,img_path,is_borrow from books where id=?"
	row := db.Db.QueryRow(sql, bookId)
	book := &model.Book{}
	row.Scan(&book.Id, &book.Title, &book.Author, &book.Publish, &book.Price, &book.Borrow, &book.Remain, &book.ImgPath, &book.IsBorrow)
	return book
}

func AddNewBook(book *model.Book) error {
	sql := "insert into books(title,author,publish,price,borrow,remain,img_path) values (?,?,?,?,?,?,?)"
	_, err := db.Db.Exec(sql, book.Title, book.Author, book.Publish, book.Price, book.Borrow, book.Remain, book.ImgPath)
	if err != nil {
		return err
	}
	return nil
}

func ReviseBook(book *model.Book) error {
	sql := "update books set title=?,author=?,publish=?,price=?,borrow=?,remain=? where id=?"
	_, err := db.Db.Exec(sql, book.Title, book.Author, book.Publish, book.Price, book.Borrow, book.Remain, book.Id)
	if err != nil {
		return err
	}
	return nil
}

func DeleteBookById(bookId int64) error {
	sql := "delete from books where id=? and is_borrow=1"
	_, err := db.Db.Exec(sql, bookId)
	if err != nil {
		return err
	}
	return nil
}

func UpdateBookNums(book *model.Book) error {
	sql := "update books set borrow=?,remain=? where id=?"
	_, err := db.Db.Exec(sql, book.Borrow, book.Remain, book.Id)
	if err != nil {
		return err
	}
	return nil
}

func UpdateBookStatus(book *model.Book) error {
	sql := "update books set is_borrow=? where id=?"
	_, err := db.Db.Exec(sql, book.IsBorrow, book.Id)
	if err != nil {
		return err
	}
	return nil
}
